package com.zhc.tools.demo.web.context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.zhc.tools.common.util.PropertiesHelper;

/**
 * 在Cookie中保存登录的用户信息的助手类
 * 
 * @author zhc
 * 
 */
public class LoginUserUtils {
	
	private static final Log LOG = LogFactory.getLog(LoginUserUtils.class);

    private static String COOKIE_NAME = "_i_u_cookie_";

    private static final String COOKIE_PATH = "/";

    private static String cookieDomain = "";

    /**
     * 从HttpServletRequest获取Cookie然后解析为登录用户的对象
     * 
     * @param request
     * @return
     */
    public static LoginUser getCookieValue(HttpServletRequest request) {

        //定义cookie name
        definedCookieName();
        String cookieValue = CookieUtils.getCookieValue(request, COOKIE_NAME);
        if (LOG.isDebugEnabled()) {
            LOG.debug("get cookie, cookieValue = " + cookieValue);
        }
        return LoginUser.parse(cookieValue);
    }

    /**
     * 设置Cookie
     * 
     * @param response
     * @param user
     */
    public static void setCookie(HttpServletResponse response, LoginUser user) {

        //定义cookie name
        definedCookieName();
        if (user != null) {
            String cookieValue = user.toString();
            if (StringUtils.isEmpty(cookieDomain)){
                cookieDomain = PropertiesHelper.newInstance().getValue("cookie.domain");
                if (StringUtils.isEmpty(cookieDomain)){
                    LOG.error(String.format("获取cookieDomain[%s]配置为空！导致setCookie[%s]失效 ", COOKIE_NAME, cookieValue));
                }
            }
            CookieUtils.setCookie(response, COOKIE_NAME, cookieValue, cookieDomain, COOKIE_PATH);

            if (LOG.isDebugEnabled()) {
            	LOG.debug(String.format("setCookie, cookName={%s}, value={%s} ", COOKIE_NAME, cookieValue));
            }
        }
    }

    /**
     * 设置Cookie失效
     * 
     * @param response
     */
    public static void invalidateCookie(HttpServletResponse response) {
        //定义cookie name
        definedCookieName();
        if (StringUtils.isEmpty(cookieDomain)){
            cookieDomain = PropertiesHelper.newInstance().getValue("cookie.domain");
            if (StringUtils.isEmpty(cookieDomain)){
                LOG.error("获取cookieDomain配置为空！导致invalidateCookie失效 ");
            }
        }
        CookieUtils.invalidateCookie(response, COOKIE_NAME, cookieDomain, COOKIE_PATH);
        if (LOG.isDebugEnabled()) {
        	LOG.debug(String.format("invalidateCookie, cookName={%s} ", COOKIE_NAME));
        }

    }

    /**
     * 定义cookieName
     * 如果子系统没定义cookieName,用默认cookieName _i_u_cookie_
     */
    private static void definedCookieName(){

        String userDefinedCookieName = PropertiesHelper.newInstance().getValue("cookie.name");
        if (StringUtils.isNotEmpty(userDefinedCookieName)){
            COOKIE_NAME = userDefinedCookieName;
        }
    }

}
